module BeverageMaker
    class Tea < Command
        self.summary = 'Drink based on cured leaves'
        self.description = <<-DESC 
            An aromatic beverage commonly prepared by pouring boiling hot
            water over cured leaves of the Camellia sinensis plant.
    
            The following flavors are available: black, green, oolong, and white.
        DESC
        # tea 后面 要跟一个 FLAVOR 参数
        self.arguments = [
            # represent individual arguments to present to
            # the command help banner

            #  def initialize(names, required, repeatable = false)
            CLAide::Argument.new('FLAVOR',true) 
        ]
        # 可选参数 help banner
        def self.options 
            [['--iced','the ice-tea version']].concat(super)
        end
        # 初始化方法
        def initialize(argv)
            # 给 实例变量 flavor 和 iced 赋值
            @flavor = argv.shift_argument 
            @iced = argv.flag?('iced') 
            super
        end

        # 检查参数是否合理
        def validate!
            super
            # 如果 flavor 变量为空，输出 help banner
            if @flavor.nil?
                help! 'A flavor argument is Required.'
            end
           # @flavor 值： 应该为 black green red 里面的一种
           # %w(black red green) == ["black", "red", "green"]
            unless %w(black red green).include?(@flavor)
                help! "#{@flavor} is not a valid favor"
            end 
        end

        def run 
            super 
            puts "* Infuse #{@flavor} tea..."
            sleep 1

            if @iced
                puts '* Cool off...'
            end
            sleep 1

            puts '* Enjoy!'
        end
    end
end